<?php
namespace app\home\controller\user;

use app\BaseController;

use app\common\model\UserBank;

use app\common\traites\PublicCrudTrait;

use think\exception\ValidateException;
use think\facade\Db;
use hg\apidoc\annotation as Apidoc;

/**
 *
 * @Apidoc\Title("个人中心")
 * */
class Bank extends BaseController
{
    protected $model;
    use PublicCrudTrait;

    public function initialize()
    {
        $this->model = new models();
        parent::initialize(); // TODO: Change the autogenerated stub
    }


    /**
     * @Apidoc\Title("银行卡列表")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("bank", type="array", desc="银行账号列表")
     */
    public function my_bank(){
        $uid= session('home_user.id');
        $UserBank= new UserBank();
        $res=$UserBank->where('user_id',$uid)->select()->toArray();
        return show($res);
    }

    /**
     * @Apidoc\Title("具体单张银行卡")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="varchar",require=true, desc="卡号")
     * @Apidoc\Returned("bank", type="array", desc="银行账号列表")
     */
    public function my_bank_one(){
        //过滤数据
        $postField = 'id';
        $params   = $this->request->only(explode(',', $postField), 'post', null);
        $UserBank= new UserBank();
        $res=$UserBank->where('id',$params['id'])->find()->toArray();
        return show($res);
    }

    /**
     * @Apidoc\Title("添加银行账号")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("card_number", type="varchar",require=true, desc="卡号")
     * @Apidoc\Param("bank_name", type="varchar",require=true, desc="银行名称")
     * @Apidoc\Param("bank_address", type="varchar",require=true, desc="银行支行名称")
     * @Apidoc\Param("true_name", type="varchar",require=true, desc="用户开卡的姓名")
     * @Apidoc\Param("is_default", type="int",require=true, desc="是否默认 0 不是 1是")
     * @Apidoc\Returned("data", type="bool", desc="返回成功失败结果")
     */
    public function add_bank(){
        $user_id= session('home_user.id');
        //过滤数据
        $postField = 'card_number,bank_name,true_name,is_default,bank_address';
        $params   = $this->request->only(explode(',', $postField), 'post', null);


        try {
            $this->validate($params,[
                //'type' => 'require|number|in:1,2', //
                'card_number' => 'require|max:100', //
                'true_name' => 'max:50', //
                'bank_address' => 'max:200', //
                'bank_name' => 'max:50', //
                'is_default' => 'require|number|in:0,1'//
                // 'password' => 'require' //
            ],
                [
                    'card_number' => 'Bank card number is required',
                    'bank_address' => 'bank address is required',
                    'bank_name' => 'Bank name is required',
                    'true_name' => 'Bank card account name is required',
                    'is_default' => 'Required by default'
                    // 'password' => 'Please fill in the login password'
                ]
            );
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return show([],config('ToConfig.http_code.error'),$e->getError());
        }

        $UserBank= new UserBank();
        $res=$UserBank->where('user_id',$user_id)->where('card_number',$params['card_number'])->find();
        if ($res){
            return show([],config('ToConfig.http_code.error'),'card number already exists');
        }

        // 验证必须 实名
        // $userRealName = (new UserRealName())->where('user_id', $user_id)->find();
        // if($userRealName['true_name'] != $params['true_name']){
        //     return show([],config('ToConfig.http_code.error'),'绑定银行卡必须同实名认证一致');
        // }


        $user_name = session('home_user.user_name');
        // $find =(new User())->where(['id'=>$user_id,'pwd' => pwdEncryption($params['password'])])->find();
        // if (empty($find)) return show([],config('ToConfig.http_code.error'),'Wrong account or password');
        // unset($params['password']);
        $count=$UserBank->where('user_id',$user_id)->value('id');

        if ($count){
            //删除原来的 覆盖上
            $UserBank->where('user_id',$user_id)->delete();
        }
        $params['user_id']=$user_id;
        $params['status']=1;
        $params['user_name']=$user_name;
        $res_bank=$UserBank->insert($params);
        if ($res_bank){
            return show([],1,'Card binding succeeded');
        }else{
            return show([],config('ToConfig.http_code.error'),'Card binding failed. Please enter the correct login password');
        }

    }

    /**
     * @Apidoc\Title("编辑银行账号")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int",require=true, desc="ID序号")
     * @Apidoc\Param("card_number", type="varchar",require=true, desc="卡号")
     * @Apidoc\Param("bank_name", type="varchar",require=true, desc="银行名称")
     * @Apidoc\Param("bank_address", type="varchar",require=true, desc="银行支行名称")
     * @Apidoc\Param("true_name", type="varchar",require=true, desc="用户开卡的姓名")
     * @Apidoc\Param("is_default", type="int",require=true, desc="是否默认 0 不是 1是")
     * @Apidoc\Returned("data", type="bool", desc="返回成功失败结果")
     */
    public function edit_bank(){
        $user_id= session('home_user.id');
        //过滤数据
        $postField = 'id,card_number,true_name,bank_address,bank_name,is_default';
        $params   = $this->request->only(explode(',', $postField), 'post', null);

        try {
            $this->validate($params,[
                'id' => 'require|number', //
                'card_number' => 'require|max:50', //
                'bank_name' => 'max:50', //
                'true_name' => 'max:50', //
                'bank_address' => 'max:200', //
                'is_default' => 'require|number|in:1,1', //
            ],
                [
                    'id' => 'ID must be passed',
                    'card_number' => 'Bank card number is required',
                    'bank_name' => 'Bank name is required',
                    'bank_address' => 'bank address name is required',
                    'true_name' => 'Bank card account name is required',
                    'is_default' => 'Required by default',
                ]
            );
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return show([],config('ToConfig.http_code.error'),$e->getError());
        }

        // 验证必须 实名
        $userRealName = (new UserRealName())->where('user_id', $user_id)->find();
        if($userRealName['true_name'] != $params['true_name']){
            return show([],config('ToConfig.http_code.error'),'绑定银行卡必须同实名认证一致');
        }

        $UserBank= new UserBank();
        $find=$UserBank->where('id',$params['id'])->find();

        if (!$find){
            return show([],config('ToConfig.http_code.error'),'Data does not exist');
        }
        $res_bank=$UserBank->update($params);
        if ($res_bank){
            return show([]);
        }else{
            return show([],config('ToConfig.http_code.error'),'failed');
        }
    }

    /**
     * @Apidoc\Title("删除银行卡")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="varchar",require=true, desc="卡号")
     * @Apidoc\Returned("bank", type="array", desc="银行账号列表")
     */
    public function del_bank() {
        //过滤数据
        $postField = 'id';
        $params = $this->request->only(explode(',', $postField), 'post', null);
        $res = UserBank::where('id', $params['id'])->findOrEmpty();
        if (!$res) {
            return show([], config('ToConfig.http_code.error'), '银行卡不存在或已删除！');
        }

        if ($res->save(['status' => -1])) {
            return show([]);
        }

        return show([], config('ToConfig.http_code.error'), '银行卡不存在或已删除！');
    }
// 类结束了
}
